home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 076-100 / 085 / lowmem / lms-example.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  4KB  |  138 lines

  1. /* :ts=8 */
  2.  
  3. /*
  4. ; Programming Example Of Using The ASDG Low Memory Server
  5. ;
  6. ; Copyright 1987 By ASDG Incorporated
  7. ;
  8. ; For  non-commercial distribution  only. Commercial distribution
  9. ; or use is  strictly  forbidden  except under license from ASDG.
  10. ; Author: Perry S. Kivolowitz
  11. ; ASDG shall in no way be held responsible for any damage or loss
  12. ; of data which may result from the use or misuse of this program
  13. ; or data. ASDG makes no warranty with respect to the correct and
  14. ; proper functioning of this code or data. However, it is the be-
  15. ; lief of ASDG that this  program and  data is  correct and shall
  16. ; function properly with correct use.
  17. ;
  18. ; These modules were written for  use  with  Manx C.  Manx C is a
  19. ; product  of  the  Manx  Software Systems company whose language
  20. ; tools are used  exclusively by  ASDG  for all its software pro-
  21. ; ducts. Yes - this is an unsolicited plug for Manx - Perry K.
  22. ;
  23. */
  24.  
  25. #include <exec/ports.h>
  26. #include "low-mem.h"
  27.  
  28. #define    MyPortName    "LowMemory Reception Port"
  29.  
  30. extern void *AllocMem();
  31. extern void *CreatePort();
  32. extern long OpenLibrary();
  33.  
  34. struct LowMemMsg LMM;
  35. struct MsgPort *LowMemoryPort = NULL;
  36. long   LowMemBase = NULL;
  37. long   LowMemSig  = 0;
  38.  
  39. /*
  40. ** In this example, we will allocate a 16K space  just so we have something
  41. ** to give up when the Low Memory Server notifies us that there's no memory
  42. ** available. We should give up something (in this example anyway) since if
  43. ** memory is REALLY tight - the printf's after the Wait may not work.
  44. */
  45.  
  46. #define    RipCordSize    16384
  47. char   *RipCord   = NULL;
  48.  
  49. CloseAll()
  50. {
  51.     if (LowMemoryPort) DeletePort(LowMemoryPort);
  52.     if (LowMemBase) CloseLibrary(LowMemBase);
  53.     if (RipCord) FreeMem(RipCord , RipCordSize);
  54.     printf("ASDG Low Memory Server Example (exiting)\n");
  55.     exit(0);
  56. }
  57.  
  58. main(argc , argv)
  59. char *argv[];
  60. {
  61.     int Result;
  62.  
  63.     if (!(RipCord = AllocMem(RipCordSize , 0L))) {
  64.         printf("Was  unable  to allocate  a rip  cord. You\n");
  65.         printf("must not have very  much memory  available\n");
  66.         printf("right now. You should consume memory while\n");
  67.         printf("this  example  is  running - NOT before it\n");
  68.         printf("starts!\n");
  69.         CloseAll();
  70.     }
  71.     printf("RipCord area has been allocated.\n");
  72.  
  73.     /* library base must be named LoeMemBase */
  74.     if (!(LowMemBase = OpenLibrary(LMSName , 0L))) {
  75.         printf("Was unable to open library: %s\n" , LMSName);
  76.         printf("Are you sure it is in ``libs:''?\n");
  77.         CloseAll();
  78.     }
  79.     printf("Successfully opened the ASDG Low Memory Server.\n");
  80.  
  81.     if (!(LowMemoryPort = CreatePort(MyPortName , 0L))) {
  82.         printf("Was unable to create a message port for use with\n");
  83.         printf("the ASDG Low Memory Server. Maybe you really are\n");
  84.         printf("low on memory?\n");
  85.         CloseAll();
  86.     }
  87.     LowMemSig = 1L << LowMemoryPort->mp_SigBit;
  88.     printf("Successfully created port whose name is: %s\n" , MyPortName);
  89.  
  90.     /* Important! Initialize lm_flag to LM_CONDITION_ACKNOWLEDGED
  91.     ** or no messages will be sent  to  you! This is the only ini-
  92.     ** tialization you need do.
  93.     */
  94.     LMM.lm_flag = LM_CONDITION_ACKNOWLEDGED;
  95.     printf("Low Memory Server Message initialized.\n");
  96.  
  97.     printf("Calling RegLowMemReq Now...\n");
  98.  
  99.     Result = RegLowMemReq(MyPortName , &LMM);
  100.  
  101.     printf("Result is: %d\n" , Result);
  102.     printf("This means: ");
  103.     switch (Result) {
  104.  
  105.     case LM_NOMEM:
  106.         printf("Not enough memory to store registration.\n");
  107.         break;
  108.  
  109.     case LM_BADNAME:
  110.         printf("Try another port name, that one is used already\n");
  111.         break;
  112.  
  113.     case 0: printf("All went well. Registration Accepted\n");
  114.         break;
  115.  
  116.     default:
  117.         printf("A bogus return value has come back to you!\n");
  118.         break;
  119.     }
  120.     if (Result < 0) CloseAll();
  121.  
  122.     printf("Waiting for you to run out of memory now.\n");
  123.     printf("Run something which will cause that situation to arise.\n");
  124.     (void) Wait(LowMemSig);
  125.  
  126.     FreeMem(RipCord , RipCordSize);
  127.     RipCord = NULL; /* don't free twice */
  128.     printf("Message received. Rip Cord Pulled\n");
  129.  
  130.     DeRegLowMemReq(MyPortName);
  131.     printf("Low memory registration has been canceled\n");
  132.  
  133.     CloseAll();
  134. }
  135.  
  136.